function Competitor(id, name, category, originalDistance, completionFlag) {
    this.id = id;
    this.name = name;
    this.category = category;
    this.originalDistance = originalDistance;
    this.completionFlag = completionFlag;
    this.lastMeasurementTime = 0;
    this.lastCheckPoint = checkPoints[0];
}

/** Returns if a competitor has finished. */
Competitor.prototype.finished = function() {
    return  (this.completionFlag=="I") ||
    (!(typeof(checkPoints[checkPoints.length - 1].measurementsByCompetitor[this.id]) === 'undefined')) && (this.completionFlag!="N")
}

/** Returns if a competitor has finished within time limit. */
Competitor.prototype.finishedWithinTimeLimit = function() {    
    if ( !this.finished() ) {
        return false;
    } else {
        try {
            if ( this.category == "55" ) {
                return checkPoints[checkPoints.length - 1].relativeTimesByCompetitor[this.id].getTime() < 14*60*60*1000;
            } else if (this.category == "88" ) {
                return checkPoints[checkPoints.length - 1 ].relativeTimesByCompetitor[this.id].getTime() < 26*60*60*1000;
            } else {        
                return checkPoints[checkPoints.length - 1 ].relativeTimesByCompetitor[this.id].getTime() < 30*60*60*1000;
            }
        } catch (err) {
            return false ;    
        }
    }
}

var competitors = new Array();
var competitorsById = new Array();
/** This Object holds an array of competitors for every category*/
var competitorsByCategory = {};

/** Fills out the competitors,competitorsById and competitorsByCategory arrays with the competitors loaded from the google spreadsheet */
function loadCompetitors(year, contMethod) {
    competitors = new Array();
    competitorsById = new Array();
    competitorsByCategory = {};
    var key = 'rNrEcj627Fk4v2E3_0hPN6A';
    Competitor['loadCallback'] = function(data) {
        var length = data.feed.entry.length;
        var currentRow = new Array() ;
        
        function pushCompetitor() {
            var competitor = new Competitor(currentRow[2], currentRow[3], currentRow[4], currentRow[6], currentRow[7]);            
            competitors.push(competitor);
            competitorsById[competitor.id] = competitor;
            var categoryArray = competitorsByCategory[competitor.category];
            if (typeof categoryArray === 'undefined') {
                categoryArray = new Array();
                competitorsByCategory[competitor.category] = categoryArray;
            }
            categoryArray.push(competitor);
        }
        
        var actualrow = 2;
        for (var i = 0; i < length; i++) {
            var entry = data.feed.entry[i];
            var col = parseInt(entry.gs$cell.col);
            var row = parseInt(entry.gs$cell.row) ;

            if (actualrow < row) {
                // new row in table: new competitor (if not header)                
                if (currentRow != null) {                    
                    pushCompetitor();
                }
                currentRow = new Array();
                actualrow = row ;
            }
            currentRow[col] = entry.content.$t ;
        }
        if (i > 7) {//hotfix, indulnom kell a Mátrába...
            pushCompetitor();
        }
        setTimeout(contMethod, 100);
    }
    $.getScript('http://spreadsheets.google.com/feeds/cells/' + key + '/'+ getWorksheetIndex(year) + '/public/values?alt=json-in-script&callback=Competitor.loadCallback');
}

